package com.onstar.cn.weight.anim;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.widget.TextView;

import com.onstar.cn.weight.R;

/**
 * 主要实现功能：显示进度条loading时，有点动画跳跃效果
 * Created by 7828wc on 2016/5/12.
 */
public class DotAnimImageView extends TextView {
    private ImageSpan dotOne;
    private ImageSpan dotTwo;
    private ImageSpan dotThree;
    private ImageSpan dotFour;

    private int showSpeed = 700;

    private int jumpHeight;
    private boolean autoPlay;
    private boolean isPlaying;
    private boolean isHide;
    private int period = 1000;
    private AnimatorSet mAnimatorSet = new AnimatorSet();
    private float textWidth;

    public DotAnimImageView(Context context) {
        super(context);
        init(context, null);
    }

    public DotAnimImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public DotAnimImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        jumpHeight =45;
        period = 1000;
        SpannableString spannable = new SpannableString(". . . .");
        Drawable d = getResources().getDrawable(R.drawable.weight_home_loading_small);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        //创建ImageSpan
        dotOne = new ImageSpan(d);
        dotTwo = new ImageSpan(d);
        dotThree = new ImageSpan(d);
        dotFour = new ImageSpan(d);

        //用ImageSpan替换文本
        spannable.setSpan(dotOne, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//        spannable.setSpan(dotTwo, 1, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        spannable.setSpan(dotTwo, 2, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        spannable.setSpan(dotThree, 4, 5, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//        spannable.setSpan(dotFour, 6, 7, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        setText(spannable, BufferType.SPANNABLE);

        textWidth = 47;

        ObjectAnimator dotOneJumpAnimator = createDotJumpAnimator(dotOne, 0);
        dotOneJumpAnimator.addUpdateListener(new InvalidateViewOnUpdate(this));

        mAnimatorSet.playTogether(dotOneJumpAnimator, createDotJumpAnimator(dotTwo,
                period / 6), createDotJumpAnimator(dotThree, period * 2 / 6));

        isPlaying = autoPlay;
        if (autoPlay && !isInEditMode()) {
            start();
        }
    }

    public void start() {
        isPlaying = true;
        setAllAnimationsRepeatCount(ValueAnimator.INFINITE);
        mAnimatorSet.start();
    }

    private ObjectAnimator createDotJumpAnimator(ImageSpan JumpTextSpan, long delay) {
        ObjectAnimator jumpAnimator = ObjectAnimator.ofFloat(JumpTextSpan, "translationY", 0, -jumpHeight);
        jumpAnimator.setEvaluator(new SinTypeEvaluator());
        jumpAnimator.setDuration(period);
        jumpAnimator.setStartDelay(delay);
        jumpAnimator.setRepeatCount(ValueAnimator.INFINITE);
        jumpAnimator.setRepeatMode(ValueAnimator.RESTART);
        return jumpAnimator;
    }

    public void stop() {
        isPlaying = false;
        setAllAnimationsRepeatCount(0);
    }

    private void setAllAnimationsRepeatCount(int repeatCount) {
        for (Animator animator : mAnimatorSet.getChildAnimations()) {
            if (animator instanceof ObjectAnimator) {
                ((ObjectAnimator) animator).setRepeatCount(repeatCount);
            }
        }
    }

    public void hide() {
        createDotHideAnimator(dotThree, 2).start();
        ObjectAnimator dotTwoMoveRightToLeft = createDotHideAnimator(dotTwo, 1);
        dotTwoMoveRightToLeft.addUpdateListener(new InvalidateViewOnUpdate(this));
        dotTwoMoveRightToLeft.start();
        isHide = true;
    }

    public void show() {
        ObjectAnimator dotThreeMoveRightToLeft = createDotShowAnimator(dotThree, 2);
        dotThreeMoveRightToLeft.start();
//        ObjectAnimator dotThreeMoveRightToLeft = createDotShowAnimator(dotThree, 2);
//        dotThreeMoveRightToLeft.start();
        ObjectAnimator dotTwoMoveRightToLeft = createDotShowAnimator(dotTwo, 1);
        dotTwoMoveRightToLeft.addUpdateListener(new InvalidateViewOnUpdate(this));
        dotTwoMoveRightToLeft.start();
        isHide = false;
    }

    private ObjectAnimator createDotHideAnimator(ImageSpan span, float widthMultiplier) {
        return createDotHorizontalAnimator(span, 0, -textWidth * widthMultiplier);
    }

    private ObjectAnimator createDotShowAnimator(ImageSpan span, int widthMultiplier) {
        return createDotHorizontalAnimator(span, -textWidth * widthMultiplier, 0);
    }

    private ObjectAnimator createDotHorizontalAnimator(ImageSpan span, float from, float to) {
        ObjectAnimator dotThreeMoveRightToLeft = ObjectAnimator.ofFloat(span, "translationX", from, to);
        dotThreeMoveRightToLeft.setDuration(showSpeed);
        return dotThreeMoveRightToLeft;
    }

    public boolean isPlaying() {
        return isPlaying;
    }

}
